package com.zwh.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwh.service.DeptService;
import com.zwh.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/crud")
public class EmpController {
    @Autowired
    private EmpService service;
    @Autowired
    private DeptService deptService;
    @GetMapping("/emps")
    @ResponseBody
    public IPage getEmps(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        IPage page=new Page(pn,7);
        service.EmpAndDept(page);
        return page;
    }


//    选中删除
    @DeleteMapping("/del/{ids}")
    @ResponseBody
    public int deleteSelected(@PathVariable("ids")String ids){
        int flag=0;
        if(ids.contains("-")){
            List<Integer> list=new ArrayList<>();
            String[] str=ids.split("-");
            for(String s:str){
                list.add(Integer.parseInt(s));
            }
         flag= service.deleteMoreById(list);
        }
        else {
            Integer id=Integer.parseInt(ids);
             flag= service.deleteById(id);
        }
        return flag;
    }
}
